package com.k2.gulimall.member.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.k2.common.utils.NewData;
import com.k2.common.vo.UserRegisterVo;
import com.k2.gulimall.member.entity.MemberEntity;
import com.k2.gulimall.member.entity.MemberStatisticsInfoEntity;
import com.k2.gulimall.member.service.MemberService;
import com.k2.gulimall.member.service.MemberStatisticsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.k2.common.utils.PageUtils;
import com.k2.common.utils.Query;

import com.k2.gulimall.member.dao.MemberLoginLogDao;
import com.k2.gulimall.member.entity.MemberLoginLogEntity;
import com.k2.gulimall.member.service.MemberLoginLogService;


@Service("memberLoginLogService")
public class MemberLoginLogServiceImpl extends ServiceImpl<MemberLoginLogDao, MemberLoginLogEntity> implements MemberLoginLogService {

    @Autowired
    MemberService memberService;
    @Autowired
    MemberStatisticsInfoService memberStatisticsInfoService;

    NewData newData = new NewData();

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberLoginLogEntity> page = this.page(
                new Query<MemberLoginLogEntity>().getPage(params),
                new QueryWrapper<MemberLoginLogEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean addmemberLoginLog(UserRegisterVo userRegisterVo) {
        try {
            MemberEntity memberEntity = new MemberEntity();
            memberEntity.setUsername(userRegisterVo.getUserName());
            //查询要修改的用户
            List<MemberEntity> getMember = memberService.getUserName(memberEntity);
            if(getMember.size()<1){
                memberEntity = memberService.getUserSocialUid(userRegisterVo.getAccessToken());
            }else{
                memberEntity = getMember.get(0);
            }
            //添加用户登陆记录
            MemberLoginLogEntity memberLoginLogEntity = new MemberLoginLogEntity();
            //用户id
            memberLoginLogEntity.setMemberId(memberEntity.getId());
            //登陆时间
            memberLoginLogEntity.setCreateTime(newData.getNewData());
            //ip
            memberLoginLogEntity.setIp(userRegisterVo.getIp()!=null?userRegisterVo.getIp():null);
            //城市
            memberLoginLogEntity.setCity(userRegisterVo.getCityname()!=null?userRegisterVo.getCityname():null);
            //登录类型
            memberLoginLogEntity.setLoginType(1);
            //保存登陆数据
            this.saveOrUpdate(memberLoginLogEntity);


            //保存统计数据
            //通过用户id查询用户的统计记录
            MemberStatisticsInfoEntity memberStatisticsInfoEntity = new MemberStatisticsInfoEntity();
            memberStatisticsInfoEntity.setMemberId(memberEntity.getId());
            List<MemberStatisticsInfoEntity> memberStatisticsInfo = memberStatisticsInfoService.getMemberIdToStatisticsInfo
                    (memberStatisticsInfoEntity);
            //修改统计记录
            if (memberStatisticsInfo!=null){
                memberStatisticsInfoEntity = memberStatisticsInfo.get(0);
            }
            try {
                if(!memberStatisticsInfoEntity.getLoginCount().equals("")&&null!=memberStatisticsInfoEntity.getLoginCount()){
                    memberStatisticsInfoEntity.setLoginCount(memberStatisticsInfoEntity.getLoginCount()+1);
                }else{
                    memberStatisticsInfoEntity.setLoginCount(1);
                }
            }catch (Exception e){
                memberStatisticsInfoEntity.setLoginCount(1);
            }
            //保存
            memberStatisticsInfoService.saveOrUpdate(memberStatisticsInfoEntity);
            return true;
        }catch (Exception e){
            return false;
        }
    }

}